﻿package 
{
    import drawing.*;
    import enum.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import image.*;
    import manager.*;
    import resource.*;
    import ui.*;
    import ui.controls.*;
    import ui.dialogs.*;

    public class Editor extends Sprite
    {
        public var area:Sprite;
        public var history:HistoryPanel;
        public var clipboard:Clipboard;
        public var tool:Tool;
        public var options:Optionbar;
        public var alt:ToolType;
        public var modal:Modal;
        public var layers:LayersPanel;
        public var hover:Hover;
        public var marker:Marker;
        public var rooturl:String = "http://www.webps.cn";
        public var kim:KeyInputManager;
        public var cursor:Cursor;
        public var navigator:NavigatorPanel;
        public var canvas:Canvas;
        public var menubar:Menubar;
        public var splash:Splash;
        public var fps:fpsBox;
        public var cm:CommandManager;
        public var tm:ToolManager;
        public var toolbar:Toolbar;

        public function Editor()
        {
            U.Init();
            if (U.url.indexOf("webps.cn") != -1 && U.url.indexOf("Editor.webps.cn") == -1)
            {
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.stageFocusRect = false;
                stage.focus = stage;
                this.kim = new KeyInputManager(this);
                this.cm = new CommandManager(this);
                this.tm = new ToolManager(this);
                this.clipboard = new Clipboard();
                this.fps = new fpsBox();
                addChild(this.fps);
                this.area = new Sprite();
                addChild(this.area);
                if (!U.IsParam("loc"))
                {
                    this.Init();
                }
                else
                {
                    this.LoadResource(U.Param("loc", true));
                }
            }
            return;
        }// end function

        public function BringToTopEvent(event:MouseEvent) : void
        {
            this.BringToTop(event.currentTarget as Box);
            return;
        }// end function

        public function TweenCanvas(param1:Canvas) : void
        {
            param1.alpha = param1.alpha + 0.25;
            if (param1.alpha < 1)
            {
                setTimeout(this.TweenCanvas, 20, param1);
            }
            else
            {
                param1.alpha = 1;
            }
            return;
        }// end function

        public function BringToTop(param1:Box) : void
        {
            setChildIndex(param1, numChildren - 5);
            return;
        }// end function

        private function ClearWorkArea() : void
        {
            stage.removeEventListener(Event.RESIZE, this.RearrangeWorkArea);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.cursor.Trace);
            if (this.canvas != null)
            {
                this.canvas.Deactivate();
            }
            removeChild(this.toolbar);
            this.toolbar = null;
            removeChild(this.navigator);
            this.navigator = null;
            removeChild(this.layers);
            this.layers = null;
            removeChild(this.history);
            this.history = null;
            removeChild(this.options);
            this.options = null;
            removeChild(this.menubar);
            this.menubar = null;
            removeChild(this.hover);
            this.hover = null;
            removeChild(this.modal);
            this.modal = null;
            removeChild(this.cursor);
            this.cursor = null;
            return;
        }// end function

        public function ActivateCanvasEvent(event:MouseEvent) : void
        {
            this.ActivateCanvas(event.currentTarget as Canvas);
            return;
        }// end function

        public function SetModal(param1:InteractiveObject, param2:Boolean = false) : void
        {
            setChildIndex(param1, numChildren - 3);
            setChildIndex(this.modal, numChildren - 4);
            if (param2)
            {
                this.modal.alpha = 0.5;
            }
            stage.focus = param1;
            if (AppState.Modald)
            {
                AppState.DblModald = true;
            }
            else
            {
                AppState.Modald = true;
            }
            return;
        }// end function

        public function RemoveCanvas() : void
        {
            if (this.canvas != null)
            {
                this.navigator.SetReference(null);
                this.history.SetReference(null);
                this.layers.SetReference(null);
                this.area.removeChild(this.canvas);
                this.canvas.UnbindTool(this.tool);
                this.canvas.Deactivate(true);
                this.canvas = null;
                if (this.area.numChildren > 0)
                {
                    this.ActivateCanvas(this.area.getChildAt((this.area.numChildren - 1)) as Canvas);
                }
                else
                {
                    this.options.minmax.visible = false;
                    this.CheckToShowSplash();
                }
            }
            return;
        }// end function

        public function CheckToShowSplash(... args) : void
        {
            if (this.area.numChildren == 0 && !U.IsParamTrue("single") && U.IsPixlr())
            {
                this.splash = new Splash(this);
            }
            return;
        }// end function

        private function LoadResource(param1:String) : void
        {
            var _loc_2:* = new URLLoader();
            _loc_2.dataFormat = URLLoaderDataFormat.TEXT;
            _loc_2.addEventListener(Event.COMPLETE, this.ParseResource);
            _loc_2.addEventListener(IOErrorEvent.IO_ERROR, this.Init);
            _loc_2.load(new URLRequest("_language/" + param1 + ".rs?rm=" + Math.random()));
            return;
        }// end function

        public function ResetPaletteLocation() : void
        {
            this.toolbar.y = 65;
            this.toolbar.x = 10;
            var _loc_1:int = 58;
            if (this.navigator.visible)
            {
                this.navigator.x = stage.stageWidth - this.navigator.width - 16;
                this.navigator.y = _loc_1;
                _loc_1 = _loc_1 + (this.navigator.height + 6);
            }
            if (this.layers.visible)
            {
                this.layers.x = stage.stageWidth - this.layers.width - 16;
                this.layers.y = _loc_1;
                _loc_1 = _loc_1 + (this.layers.height + 6);
            }
            if (this.history.visible)
            {
                this.history.x = stage.stageWidth - this.history.width - 16;
                this.history.y = _loc_1;
                _loc_1 = _loc_1 + (this.history.height + 6);
            }
            return;
        }// end function

        public function SetLanguage(param1:String) : void
        {
            this.ClearWorkArea();
            this.LoadResource(param1);
            return;
        }// end function

        private function SetupWorkArea() : void
        {
            this.hover = new Hover(this);
            this.modal = new Modal();
            addChildAt(this.modal, 0);
            this.toolbar = new Toolbar(this);
            this.toolbar.addEventListener(MouseEvent.MOUSE_DOWN, this.BringToTopEvent);
            addChild(this.toolbar);
            this.navigator = new NavigatorPanel(this);
            this.navigator.addEventListener(MouseEvent.MOUSE_DOWN, this.BringToTopEvent);
            addChild(this.navigator);
            this.layers = new LayersPanel(this);
            this.layers.addEventListener(MouseEvent.MOUSE_DOWN, this.BringToTopEvent);
            addChild(this.layers);
            this.history = new HistoryPanel(this);
            this.history.addEventListener(MouseEvent.MOUSE_DOWN, this.BringToTopEvent);
            addChild(this.history);
            this.options = new Optionbar(this);
            this.options.y = 22;
            addChild(this.options);
            this.menubar = new Menubar(this);
            addChild(this.menubar);
            addChild(this.hover);
            this.cursor = new Cursor(this);
            addChild(this.cursor);
            this.RearrangeWorkArea();
            this.ResetPaletteLocation();
            return;
        }// end function

        public function Minimize(... args) : void
        {
            AppState.Maximized = false;
            this.options.minmax.visible = false;
            this.OrderArea();
            return;
        }// end function

        private function Init(... args) : void
        {
            this.SetupWorkArea();
            this.toolbar.SelectTool(ToolType.Marquee);
            stage.addEventListener(Event.RESIZE, this.RearrangeWorkArea);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.cursor.Trace);
            if (U.IsParam("image"))
            {
                new OpenImageDialog(this, U.Param("image"), U.Param("title"), true);
            }
            else
            {
                this.CheckToShowSplash();
            }
            return;
        }// end function

        public function AddCanvas(param1:Canvas) : void
        {
            param1.alpha = 0;
            param1.x = 85 + 25 * this.area.numChildren;
            param1.y = 58 + 25 * this.area.numChildren;
            if (param1.y > stage.stageHeight - 20)
            {
                param1.y = 85;
            }
            this.area.addChild(param1);
            var _loc_2:* = stage.stageWidth - param1.x - 240 > param1.dw + 24 ? (param1.dw) : (stage.stageWidth - param1.x - 264);
            var _loc_3:* = stage.stageHeight - param1.y - 10 > param1.dh + 42 ? (param1.dh) : (stage.stageHeight - param1.y - 52);
            if (param1.dw / _loc_2 > param1.dh / _loc_3)
            {
                _loc_3 = int(param1.dh * (_loc_2 / param1.dw));
            }
            else
            {
                _loc_2 = int(param1.dw * (_loc_3 / param1.dh));
            }
            param1.Resize(_loc_2 + 24, _loc_3 + 42);
            this.ActivateCanvas(param1);
            this.cm.ZoomShowAll(true);
            setTimeout(this.TweenCanvas, 10, param1);
            return;
        }// end function

        private function ParseResource(event:Event) : void
        {
            U.ParseResource(event.target.data as String);
            setTimeout(this.Init, 10);
            return;
        }// end function

        public function ActivateCanvas(param1:Canvas) : void
        {
            if (this.canvas != null)
            {
                this.canvas.UnbindTool(this.tool);
                this.canvas.Deactivate();
            }
            this.canvas = param1;
            this.canvas.Activate();
            this.area.setChildIndex(this.canvas, (this.area.numChildren - 1));
            this.navigator.SetReference(this.canvas);
            this.history.SetReference(this.canvas.history);
            this.layers.SetReference(this.canvas.document);
            this.canvas.BindTool(this.tool);
            if (AppState.Maximized)
            {
                this.MaxMinCanvas();
                this.options.UpdateCanvasList();
            }
            if (this.canvas.masking.hitTestPoint(stage.mouseX, stage.mouseY))
            {
                this.canvas.CanvasOver();
            }
            stage.focus = this.canvas;
            return;
        }// end function

        public function RearrangeWorkArea(... args) : void
        {
            if (this.menubar != null)
            {
                this.menubar.Resize();
            }
            if (this.options != null)
            {
                this.options.Resize();
            }
            if (this.splash != null)
            {
                this.splash.Center();
            }
            if (this.modal != null)
            {
                this.modal.Resize();
            }
            if (AppState.Maximized)
            {
                this.MaxMinCanvas();
            }
            this.fps.x = stage.stageWidth - 100;
            this.fps.y = stage.stageHeight - 30;
            return;
        }// end function

        public function OrderArea() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Canvas = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            while (_loc_1 < this.area.numChildren)
            {
                
                _loc_2 = this.area.getChildAt(_loc_1) as Canvas;
                _loc_2.x = 85 + 25 * _loc_1;
                _loc_2.y = 58 + 25 * _loc_1;
                _loc_3 = stage.stageWidth - _loc_2.x - 240 > _loc_2.dw + 24 ? (_loc_2.dw) : (stage.stageWidth - _loc_2.x - 264);
                _loc_4 = stage.stageHeight - _loc_2.y - 10 > _loc_2.dh + 42 ? (_loc_2.dh) : (stage.stageHeight - _loc_2.y - 52);
                if (_loc_2.dw / _loc_3 > _loc_2.dh / _loc_4)
                {
                    _loc_4 = int(_loc_2.dh * (_loc_3 / _loc_2.dw));
                }
                else
                {
                    _loc_3 = int(this.canvas.dw * (_loc_4 / this.canvas.dh));
                }
                _loc_2.Resize(_loc_3 + 24, _loc_4 + 42);
                _loc_2.UpdateHolderSize();
                _loc_1++;
            }
            return;
        }// end function

        public function MaxMinCanvas() : void
        {
            if (AppState.Maximized && this.area.numChildren > 0 && this.canvas != null)
            {
                this.canvas.x = -4;
                this.canvas.y = 28;
                this.canvas.Resize(stage.stageWidth + 8, stage.stageHeight - 24);
                this.canvas.UpdateHolderSize();
                this.options.minmax.visible = true;
                this.options.visible = true;
            }
            return;
        }// end function

        public function RemoveModal() : void
        {
            if (AppState.DblModald)
            {
                AppState.DblModald = false;
                this.modal.alpha = 0;
                setChildIndex(this.modal, numChildren - 4);
            }
            else
            {
                setChildIndex(this.modal, 0);
                this.modal.alpha = 0;
                AppState.Modald = false;
            }
            if (this.canvas)
            {
                stage.focus = this.canvas;
            }
            else
            {
                stage.focus = stage;
            }
            return;
        }// end function

        public function SetTool(param1:ToolType) : void
        {
            if (!this.cursor.freeze)
            {
                if (this.tool != null)
                {
                    this.tool.CleanUp();
                }
                if (this.canvas != null)
                {
                    this.canvas.UnbindTool(this.tool);
                }
                this.tool = this.tm.InstanceTool(param1);
                if (this.canvas != null)
                {
                    this.canvas.BindTool(this.tool);
                }
                this.options.SetTool(param1);
                if (AppState.OnCanvas)
                {
                    this.cursor.Set(param1);
                }
            }
            return;
        }// end function

    }
}
